package com.example.demo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

import static java.lang.Thread.interrupted;

public class BasicDialogActivity extends AppCompatActivity {

    private Button btnAlertDialog;
    private Button btnRadioDialog;
    private Button btnCheckBoxDialog;
    private Button btnListDialog;
    private Button btnProgressDialog;
    private Button btnDragDialog;
    private Button btnTimePickerDialog;
    private Button btnDatePickerDialog;
    private Button btnCustomDialog;

    private int index;  //用于保存单选对话框中选中的项的索引下标
    private boolean[] cb_checked;  //用于保存复选对话框的每一项的选中状态
    String[] cities;

    private int m_year;
    private int m_month;
    private int m_dayOfMonth;
    private int m_hourOfDay;
    private int m_minute;

    private Calendar calendar = Calendar.getInstance();

    private Thread downloadThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_dialog);
        initView();

    }


    private void initView() {
        btnAlertDialog = findViewById(R.id.btn_alertDialog);
        btnAlertDialog.setOnClickListener(new MyButtonClickListener());

        btnRadioDialog = findViewById(R.id.btn_radioDialog);
        btnRadioDialog.setOnClickListener(new MyButtonClickListener());

        btnCheckBoxDialog = findViewById(R.id.btn_checkBoxDialog);
        btnCheckBoxDialog.setOnClickListener(new MyButtonClickListener());

        btnProgressDialog = findViewById(R.id.btn_processDialog);
        btnProgressDialog.setOnClickListener(new MyButtonClickListener());

        btnListDialog = findViewById(R.id.btn_listDialog);
        btnListDialog.setOnClickListener(new MyButtonClickListener());

        btnTimePickerDialog = findViewById(R.id.btn_timePickerDialog);
        btnTimePickerDialog.setOnClickListener(new MyButtonClickListener());

        btnDatePickerDialog = findViewById(R.id.btn_datePickerDialog);
        btnDatePickerDialog.setOnClickListener(new MyButtonClickListener());

        btnCustomDialog = findViewById(R.id.btn_customDialog);
        btnCustomDialog.setOnClickListener(new MyButtonClickListener());

        btnDragDialog = findViewById(R.id.btn_dragDialog);
        btnDragDialog.setOnClickListener(new MyButtonClickListener());

        cities = getResources().getStringArray(R.array.city_array);
    }

    private class MyButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_alertDialog:
                    new AlertDialog.Builder(BasicDialogActivity.this)
                            .setTitle("注意")
                            .setMessage("确定要退出？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            })
                            .setNegativeButton("取消", null)
                            .create()
                            .show();
                    break;
                case R.id.btn_radioDialog:
                    AlertDialog.Builder builder = new AlertDialog.Builder(BasicDialogActivity.this);
                    builder.setTitle("请选择性别");
                    builder.setCancelable(false);
                    builder.setSingleChoiceItems(R.array.sex_array, -1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            index = which;
                            String content = getResources().getStringArray(R.array.sex_array)[which];
                            Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String content = getResources().getStringArray(R.array.sex_array)[index];
                            Toast.makeText(getApplicationContext(), "你选择的是" + content, Toast.LENGTH_SHORT).show();
                        }
                    });
                    builder.create().show();
                    break;
                case R.id.btn_checkBoxDialog:
                    StringBuilder result = new StringBuilder();
                    cb_checked = new boolean[cities.length];
                    new AlertDialog.Builder(BasicDialogActivity.this)
                            .setTitle("请选择城市")
                            .setMultiChoiceItems(cities, null, new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                    cb_checked[which] = isChecked;
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    for (int i = 0; i < cb_checked.length; i++) {
                                        if (cb_checked[i]) {
                                            result.append(cities[i] + " ");
                                        }
                                    }
                                    Toast.makeText(BasicDialogActivity.this, "你选择的是：" + result.toString(), Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNegativeButton("取消", null)
                            .create()
                            .show();
                    break;
                case R.id.btn_listDialog:
                    new AlertDialog.Builder(BasicDialogActivity.this)
                            .setItems(cities, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    String content = "你点击的是：" + cities[which];
                                    Toast.makeText(BasicDialogActivity.this, content, Toast.LENGTH_SHORT).show();
                                }
                            })
                            .create().show();

                    break;
                case R.id.btn_dragDialog:
                    View seekView = getLayoutInflater().inflate(R.layout.seek_drag_layout, null);
                    SeekBar seekBar = seekView.findViewById(R.id.seekBar);
                    seekBar.setMax(100);
                    seekBar.setProgress(0);
                    final TextView tvSeekBar = seekView.findViewById(R.id.tv_seekBar);
                    tvSeekBar.setText("当前进度为：" + seekBar.getProgress());
                    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                            tvSeekBar.setText("当前进度为：" + seekBar.getProgress());
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                        }
                    });
                    new AlertDialog.Builder(BasicDialogActivity.this)
                            .setTitle("拖动条对话框")
                            .setView(seekView)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(BasicDialogActivity.this, "拖动条进度为：" + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
                                }
                            })
                            .create().show();
                    break;
                case R.id.btn_processDialog:
                    ProgressDialog pd = new ProgressDialog(BasicDialogActivity.this);
                    pd.setTitle("文件正在下载中");
                    pd.setIcon(R.mipmap.ic_launcher);
                    pd.setMax(100);
                    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    pd.setCancelable(true);
                    pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            Log.i("操作", "点击Back按钮");
                            downloadThread.interrupt();
                        }
                    });
                    pd.setCanceledOnTouchOutside(false);
                    pd.show();
                    downloadThread = new Thread() {
                        @Override
                        public void run() {
                            int progress = 0;
                            while (progress < 100) {
                                System.out.println(this.isInterrupted());
                                try {
                                    Thread.sleep(1000);
                                    progress += 10;
                                    Log.i("文件下载情况", progress + "%");
                                    pd.incrementProgressBy(10);
                                } catch (InterruptedException e) {
                                    Log.i("文件下载情况", "下载停止！");
                                    e.printStackTrace();
                                    break;
                                }
                            }
                        }
                    };
                    downloadThread.start();
                    break;

                case R.id.btn_datePickerDialog:
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    m_year = calendar.get(Calendar.YEAR);
                    m_month = calendar.get(Calendar.MONTH);
                    m_dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                    DatePickerDialog datePickerDialog = new DatePickerDialog(BasicDialogActivity.this,
                            new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                    m_year = year;
                                    m_month = month;
                                    m_dayOfMonth = dayOfMonth;
                                    String content = year + "年" + (month + 1) + "月" + dayOfMonth + "日";
                                    Toast.makeText(BasicDialogActivity.this, "你设置的日期是：" + content, Toast.LENGTH_SHORT).show();

                                }
                            }, m_year, m_month, m_dayOfMonth);
                    datePickerDialog.setTitle("请选择日期");
                    datePickerDialog.setIcon(R.mipmap.ic_launcher);
                    datePickerDialog.show();
                    break;

                case R.id.btn_timePickerDialog:
                    m_hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
                    m_minute = calendar.get(Calendar.MINUTE);
                    TimePickerDialog timePickerDialog = new TimePickerDialog(BasicDialogActivity.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            m_hourOfDay = hourOfDay;
                            m_minute = minute;
                            Toast.makeText(BasicDialogActivity.this, "你选择的时间是： " + hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
                        }
                    }, m_hourOfDay, m_minute, true);
                    timePickerDialog.setIcon(R.mipmap.ic_launcher);
                    timePickerDialog.setMessage("请选择时间");
                    timePickerDialog.show();
                    break;
                case R.id.btn_customDialog:
                    //创建自定义的Dialog对象
                    final CustomDialog customDialog = new CustomDialog(BasicDialogActivity.this);
                    //设置对话框图标
                    customDialog.setIcon(R.mipmap.ic_launcher);
                    customDialog.setTitle("自定义对话框");
                    customDialog.setCanceledOnTouchOutside(false);
                    customDialog.show();
                    //通过对话框找到相关控件
                    Button btnCancel = customDialog.findViewById(R.id.btn_cancel);
                    Button btnSubmit = customDialog.findViewById(R.id.btn_submit);
                    final EditText etUsername = customDialog.findViewById(R.id.et_username);
                    final EditText etPassword = customDialog.findViewById(R.id.et_password);

                    //设置按钮事件
                    btnCancel.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            customDialog.cancel();
                        }
                    });
                    btnSubmit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String name = etUsername.getText().toString();
                            String password = etPassword.getText().toString();
                            Toast.makeText(BasicDialogActivity.this, "用户名：" + name + ",密码：" + password, Toast.LENGTH_SHORT).show();

                        }
                    });
                    break;
            }
        }

    }

}